package com.whut.googleplay.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.whut.googleplay.holder.BaseHolder;
import com.whut.googleplay.holder.MoreHolder;
import com.whut.googleplay.manager.ThreadManager;
import com.whut.googleplay.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * ============================================================
 * <br/>
 * 作 者 :        xyb
 * <br/>
 * 版 本 :        1.0
 * <br/>
 * 创建日期 ：     2016-04-27 下午4:54
 * <br/>
 * 描 述 ：
 * <br/>
 * 修订历史 ：
 * <br/>
 * ============================================================
 **/
public abstract class CustomAdapter<T> extends BaseAdapter implements AbsListView.RecyclerListener{

    public ListView mListView;
    public List mDataList;
    private List<BaseHolder> mDisplayHolderList;

    public CustomAdapter(ListView listView, List<T> dataList) {
        this.mListView = listView;

        //初始化一个界面可以看见的holder
        mDisplayHolderList = new ArrayList<BaseHolder>();
        if(mListView!=null){
            mListView.setRecyclerListener(this);
        }

        setData(dataList);
    }

    public void setData(List<T> mDatas) {
        this.mDataList = mDatas;
    }

    public List<T> getData() {
        return mDataList;
    }


    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Object getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BaseHolder baseHolder = null;
        if(convertView == null){
            //需要加载更多
            if(getItemViewType(position) == MORE_ITEM_TYPE){
                baseHolder = getMoreHolder();
            }else{
                baseHolder =  getHolder();
            }
        }else{
            baseHolder = (BaseHolder) convertView.getTag();
        }
        if(getItemViewType(position) == ITEM_VIEW_TYPE){
            baseHolder.setData(getItem(position));
        }
        //这个有毛用
        mDisplayHolderList.add(baseHolder);
        return baseHolder.getRootView();
    }

    public abstract BaseHolder getHolder() ;

    // 表示加载更多
    private final int MORE_ITEM_TYPE = 0;
    // 表示普通的数据类型
    private final int ITEM_VIEW_TYPE = 1;
    private MoreHolder moreHolder;

    /**
     * 获取当前item的类型,如果是当前的最后一个
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        if(position == getCount() -1){
            return MORE_ITEM_TYPE;
        }else {
            return getInnerItemViewType(position);
        }
    }

    public int getInnerItemViewType(int position) {
        return ITEM_VIEW_TYPE;
    }

    /**
     * 总共有几个类型
     * @return
     */
    @Override
    public int getViewTypeCount() {
        return super.getViewTypeCount()+1;
    }


    /**
     * 获取更多的holder
     * @return
     */
    private BaseHolder getMoreHolder() {
        if(moreHolder == null){
            moreHolder = new MoreHolder(hasMore(),this);
        }

        return moreHolder;
    }
    /**
     * 表示有更多的数据
     * @return
     */
    public boolean hasMore() {
        // TODO Auto-generated method stub
        return true;
    }

    /**
     * 当listview回收的时候调用当前的方法
     */
    @Override
    public void onMovedToScrapHeap(View view) {
        System.out.println("我被回收了.....");
        if (null != view) {
            BaseHolder holder = (BaseHolder) view.getTag();
            if (null != holder) {
                synchronized (mDisplayHolderList) {
                    mDisplayHolderList.remove(holder);
                }

            }
        }

    }
    private boolean isLoading = false;
    /**
     * 加载更多
     */
    public void loadMore() {
        if(!isLoading){
            isLoading = true;
            ThreadManager.getLongPool().execute(new Runnable() {

                @Override
                public void run() {
                    final List list =  onLoadMore();
                    UIUtils.runInMainThread(new Runnable() {

                        @Override
                        public void run() {
                            if (null == list) {
                                //如果服务器返回的数据等于null。那么设置一个错误状态
                                getMoreHolder().setData(MoreHolder.ERROR);
                            } else if (list.size() < 20) {
                                //如果服务器返回的数据小于20条。那么设置没有更多数据的状态
                                getMoreHolder().setData(MoreHolder.NO_MORE);
                            } else {
                                getMoreHolder().setData(MoreHolder.HAS_MORE);
                            }

                            if (null != list) {
                                if (null != mDataList) {
                                    mDataList.addAll(list);
                                } else {
                                  setData(list);
                                }
                            }
                            //刷新界面
                            notifyDataSetChanged();
                            isLoading = false;
                        }
                    });

                }
            });
        }



    }

    protected abstract List onLoadMore();


}
